package com.example.framework509.common.utils;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Objects;

@Component
public class FileUtil {

    private static final String HeaderPath = "E:\\headerServer\\";

    private static final String HeadUrl = "http://192.168.66.191:8085/header/upload/";

    public String headerNameGen(String userName,String extension){
        String name = userName + new Date().getTime()+extension;
        return name;
    }

    public String getFileExtension(MultipartFile File) {
        String originalFileName = File.getOriginalFilename();
        return originalFileName.substring(originalFileName.lastIndexOf("."));
    }

    //存储后返回文件地址
    public String storageHeader(MultipartFile file,String userName){
        if (Objects.isNull(file) || file.isEmpty()) {
            return null;
        }
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(HeaderPath);
            if (!Files.isWritable(path)) {
                Files.createDirectories(path);
            }
            String extension = getFileExtension(file);
            String fileName = headerNameGen(userName,extension);
            File fileStore = new File(HeaderPath+fileName);
            fileOutputStream = new FileOutputStream(fileStore);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bufferedOutputStream.write(bytes);

            return HeadUrl+fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally
        {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Boolean storageExsitFile(File file,String type){

        String destDir = HeaderPath;

        if (type.equals("Pak")){
            destDir += "Pak\\";
        }else if (type.equals("BluePrint")){
            destDir += "BluePrint\\";
        }

        File fileDir = new File(destDir) ;
        try {
            FileUtils.copyFileToDirectory(file,fileDir);
        }
        catch (IOException e) {
            return false;
        }
        return true;
    }


    public void close(Closeable... closeables) {
        if (closeables == null || closeables.length == 0) {
            return;
        }
        for (Closeable closeable : closeables) {
            if (closeable == null) {
                continue;
            }
            try {
                closeable.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
